import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
  console.log('开始插入菜单配置数据...');

  // 插入主菜单
  const mainMenus = [
    { key: 'dashboard', label: '仪表板', icon: 'DashboardOutlined', path: '/dashboard', sortOrder: 1 },
    { key: 'knowledge', label: '知识库管理', icon: 'BookOutlined', path: '/knowledge', sortOrder: 2 },
    { key: 'users', label: '用户管理', icon: 'UserOutlined', path: '/users', sortOrder: 3 },
    { key: 'categories', label: '分类管理', icon: 'FolderOutlined', path: '/categories', sortOrder: 4 },
    { key: 'tags', label: '标签管理', icon: 'TagsOutlined', path: '/tags', sortOrder: 5 },
    { key: 'files', label: '文件管理', icon: 'FileOutlined', path: '/files', sortOrder: 6 },
    { key: 'analytics', label: '数据统计', icon: 'BarChartOutlined', path: '/analytics', sortOrder: 7 },
    { key: 'settings', label: '系统设置', icon: 'SettingOutlined', path: '/settings', sortOrder: 8 }
  ];

  for (const menu of mainMenus) {
    await prisma.menuConfig.upsert({
      where: { key: menu.key },
      update: menu,
      create: menu
    });
    console.log(`菜单 ${menu.label} 已插入/更新`);
  }

  console.log('开始插入角色权限数据...');

  // 获取所有菜单
  const allMenus = await prisma.menuConfig.findMany();

  // ADMIN 角色权限（全部菜单可见）
  for (const menu of allMenus) {
    await prisma.roleMenuPermission.upsert({
      where: { role_menuKey: { role: 'ADMIN', menuKey: menu.key } },
      update: { canView: true, canEdit: true, canDelete: true },
      create: { role: 'ADMIN', menuKey: menu.key, canView: true, canEdit: true, canDelete: true }
    });
  }
  console.log('ADMIN 角色权限已设置');

  // EDITOR 角色权限（除了用户管理和数据统计）
  const editorMenus = allMenus.filter(menu => !['users', 'analytics'].includes(menu.key));
  for (const menu of editorMenus) {
    await prisma.roleMenuPermission.upsert({
      where: { role_menuKey: { role: 'EDITOR', menuKey: menu.key } },
      update: { 
        canView: true, 
        canEdit: menu.key === 'knowledge' || menu.key === 'categories' || menu.key === 'tags' || menu.key === 'files', 
        canDelete: menu.key === 'knowledge' 
      },
      create: { 
        role: 'EDITOR', 
        menuKey: menu.key, 
        canView: true, 
        canEdit: menu.key === 'knowledge' || menu.key === 'categories' || menu.key === 'tags' || menu.key === 'files', 
        canDelete: menu.key === 'knowledge' 
      }
    });
  }
  console.log('EDITOR 角色权限已设置');

  // VIEWER 角色权限（只读权限）
  const viewerMenus = allMenus.filter(menu => !['users', 'analytics'].includes(menu.key));
  for (const menu of viewerMenus) {
    await prisma.roleMenuPermission.upsert({
      where: { role_menuKey: { role: 'VIEWER', menuKey: menu.key } },
      update: { canView: true, canEdit: false, canDelete: false },
      create: { role: 'VIEWER', menuKey: menu.key, canView: true, canEdit: false, canDelete: false }
    });
  }
  console.log('VIEWER 角色权限已设置');

  console.log('所有数据插入完成！');
}

main()
  .catch((e) => {
    console.error('种子脚本执行失败:', e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
